define(function(require, exports, module) {

var $ = require('$'),
    Widget = require('widget'),
    moment = require('moment');

require('daterangepicker');

var DateRange = Widget.extend({

  defaults: {
    classPrefix: 'datepicker',
    element: '<input class="form-control" type="text" placeholder="开始时间至结束时间" readonly/>',
    startTimeName: 'startTime',
    endTimeName: 'endTime'
  },

  setup: function() {
    this.render();
    this.initPicker();
  },

  initPicker: function() {
    var self = this,
        startTime, endTime,
        startTimeVal, endTimeVal,
        startTimeName = this.option('startTimeName'),
        endTimeName = this.option('endTimeName');

    startTime = self.element.closest('form').find('[name=' + startTimeName + ']');
    endTime = self.element.closest('form').find('[name=' + endTimeName + ']');

    if (!startTime.length) {
      startTime = $('<input type="hidden" name="' + startTimeName + '">').insertAfter(this.element);
    }

    if (!endTime.length) {
      endTime = $('<input type="hidden" name="' + endTimeName + '">').insertAfter(this.element);
    }

    self.element.after(' ');

    startTimeVal = startTime.val();
    endTimeVal = endTime.val();

    if (startTimeVal && endTimeVal) {
      self.element.val(moment(+startTimeVal).format('YYYY-MM-DD') +
          '至' + moment(+endTimeVal).format('YYYY-MM-DD'));
    }

    self.element.daterangepicker({
      //minDate: moment().subtract('years', 1),
      maxDate: moment(),
      opens: self.option('opens'),
      format: 'YYYY-MM-DD',
      separator:'至',
      showDropdowns: true,
      ranges: {
         '今天': [moment(), moment()],
         '昨天': [moment().subtract('days', 1), moment().subtract('days', 1)],
         '近7天': [moment().subtract('days', 6), moment()],
         '近30天': [moment().subtract('days', 29), moment()],
         '当月': [moment().startOf('month'), moment().endOf('month')],
         '上个月': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
      },
      locale: {
        applyLabel: '确定',
        cancelLabel: '清除',
        fromLabel: '开始',
        toLabel: '结束',
        customRangeLabel: '自定义',
        daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
        monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        firstDay: 0
      }
    }).on('apply.daterangepicker', function(ev, picker) {
      startTime.val(picker.startDate.valueOf());
      endTime.val(picker.endDate.valueOf());
    }).on('cancel.daterangepicker', function(ev, picker) {
      startTime.val('');
      endTime.val('');
      picker.element.val('');
      picker.startDate = moment();
      picker.endDate = moment();
      picker.updateCalendars();
    });

    self.picker = self.element.data('daterangepicker');
  },

  setStartTime: function (startTime) {
    this.picker.setStartDate(startTime);
  },

  setEndTime: function (endTime) {
    this.picker.setEndDate(endTime);
  },

  destroy: function () {
    this.picker && this.picker.remove();

    DateRange.superclass.destroy.apply(this);
  }

});

module.exports = DateRange;

});
